﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.Graphics;

namespace Duckbill
{
    public enum ObjectType
    {
        none,
        tile,
        transition,
        player,
        enemy,
        playerAttack,
        enemyAttack,
    }

    public class GameObject
    {
        public static Dictionary<string, Sprite> loadedSprites = new Dictionary<string,Sprite>();

        public bool toRemove = false;
        public ObjectType type = ObjectType.none;
        public Vector2 pos = new Vector2(0, 0);

        public Room room = null;
        
        public Sprite sprite = null;
        public bool visible = true;
        public float angle = 0f;
        public float depth = 0.5f;

        public static void LoadSprites(ContentManager cMan)
        {
            string[,] toLoad = {    //firstArg = internalName, secondArg = fileName
                                   {"null", "empty"},
                                   {"tile1", "Tile1"},
                                   {"tile2", "Tile2"},
                                   {"player", "stick"},
                                   {"gunTile", "gun"},
                                   {"bullet", "bullet"},
                                   {"enemy", "enemy"},
                                   {"title", "title"},
                               };
            for (int i = 0; i < toLoad.GetLength(0); i++)
                loadedSprites[toLoad[i, 0]] = new Sprite(cMan, toLoad[i, 1]);
        }

        public GameObject(string sprite, Vector2 pos)
        {
            if (sprite != null)
            {
                try
                {
                    this.sprite = new Sprite(loadedSprites[sprite]);
                }
                catch (KeyNotFoundException)
                {
                    this.sprite = new Sprite();
                }
            }
            this.pos = pos;
        }

        public Rectangle getRect()
        {
            if (sprite != null)
                return new Rectangle((int)pos.X, (int)pos.Y, (int)sprite.size.X, (int)sprite.size.Y);
            return Rectangle.Empty;
        }

        public virtual void Update(float time, Dictionary<ObjectType, List<GameObject>> others) { }
        public virtual void Move(float time) { }
        public virtual void Draw(SpriteBatch batch)
        {
            if (sprite != null && visible)
            {
                if (room != null)
                    sprite.Draw(batch, pos-room.Camera, angle, depth);
                else
                    sprite.Draw(batch, pos, angle, depth);
            }
        }
    }
}
